/**
 * Package	: com.coship.vasms.adstorm.job
 * File		: DeleteDirtyDataJob.java
 * Company 	: Shenzhen Coship Electronics CO., LTD.
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Copyright (c) 2003-2009 Shenzhen Coship Electronics CO., LTD.
 * Rainbow Bldg.,North,Hi-Tech Industrial Park,Nanshan District,Shenzhen,China,P.C.518057.
 * All rights reserved.
 * The contents of this file are confidential and proprietary to COSHIP.
 * Copying is explicitly prohibited without the express permission of COSHIP.
 *
 * Create Date	: 2010-5-10
 * Create By    : 904004
 * History :$
 */
package com.coship.vasms.adstorm.job;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.coship.vasms.adstorm.common.BeanHelper;

/** 
 * 
 * File Name    : DeleteDirtyDataJob.java
 * @Description 	: 定时任务，删除广告系统中的过期数据
 * @author 904004
 */
public class DeleteDirtyDataJob implements StatefulJob{

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger
		.getLogger(ADNotifyPlayJob.class);
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		logger.info("开始定时任务：删除广告系统中的过期数据");
		SqlMapClientTemplate smct  =  (SqlMapClientTemplate)BeanHelper.getInstance().getBean("sqlMapClientTemplate");
		DataSource ds = smct.getDataSource();
		Connection con = null;
		Map<String, String> sqlMap = context.getMergedJobDataMap();
		Iterator<String> it;
		it = (sqlMap == null)?null:sqlMap.keySet().iterator();
		try {
			con = ds.getConnection();
			if(ds != null){
				con.setAutoCommit(false);
				Statement st = con.createStatement();
				
				while(it != null && it.hasNext()){
					st.addBatch(sqlMap.get(it.next()));
				}
				st.executeBatch();
				con.commit();
				logger.info("删除广告系统中的过期数据成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.warn("定时任务执行时出错", e);
			if(con != null){
				try {
					con.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}finally{
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		logger.info("定时任务结束：删除广告系统中的过期数据");
	}
	public void test(){
		logger.info("开始定时任务：删除广告系统中的过期数据");
		SqlMapClientTemplate smct  =  (SqlMapClientTemplate)BeanHelper.getInstance().getBean("sqlMapClientTemplate");
		DataSource ds = smct.getDataSource();
		Connection con = null;
		Map<String, String> sqlMap = new HashMap<String, String>();
		sqlMap.put("sql1", "delete from accesslog where logId=2");
		sqlMap.put("sql2", "delete from accesslog where logId=3");
		sqlMap.put("sql3", "delete from accesslog where dffflogId=4");
		sqlMap.put("sql4", "delete from accesslog where logId=24");
//		Iterator<String> it = sqlMap.keySet().iterator();
		Iterator<String> it = null;
		try {
			con = ds.getConnection();
			if(ds != null){
				con.setAutoCommit(false);
				Statement st = con.createStatement();
				while(it != null && it.hasNext()){
					st.addBatch(sqlMap.get(it.next()));
				}
				logger.info("任务结束：删除广告系统中的过期数据成功");
				st.executeBatch();
				con.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.warn("定时任务执行时出错", e);
			if(con != null){
				try {
					con.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}finally{
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		logger.info("定时任务结束：删除广告系统中的过期数据");
	}
	public static void main(String args[]){
		DeleteDirtyDataJob ddj = new DeleteDirtyDataJob();
		ddj.test();
	}
}
